﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 零件服务
    /// </summary>
    public class MaterialPartsService : IMaterialPartsService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_Parts model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Updateable<T_Material_Parts>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Material_Parts GetById(string id)
        {
            return dal.Queryable<T_Material_Parts, T_Supplier>((a, b) => new object[]
            {
                JoinType.Left,a.SupplierID == b.ID,
            })
            .Where((a, b) => a.ID == id)
            .Select((a, b) => new T_Material_Parts()
            {
                ID = a.ID,
                CompanyID = a.CompanyID,
                PartCode = a.PartCode,
                PartName = a.PartName,
                SupplierID = a.SupplierID,
                SupplierName = b.SupplierName,
                RoHSTestReportID = a.RoHSTestReportID,
                MCDID = a.MCDID,
                GreenStatus = a.GreenStatus,
                DataStatus = a.DataStatus,
                CreateTime = a.CreateTime
            }).First();
        }

        public List<T_Material_Parts> GetList(string companyID, string supplierID)
        {
            Expression<Func<T_Material_Parts, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID.StartsWith(companyID), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(supplierID))
            {
                whereExp = whereExp.Compose((a) => a.SupplierID.StartsWith(supplierID), Expression.AndAlso);
            }
            return dal.Queryable<T_Material_Parts>().Where(whereExp).ToList();
        }

        public PageData<T_Material_Parts> Query(string companyID, string supplierID, string partCode, string partName, PageInfo page)
        {
            int totalCount = 0;
            Expression<Func<T_Material_Parts, T_Supplier, bool>> whereExp = (a, b) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a, b) => a.CompanyID == companyID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(supplierID))
            {
                whereExp = whereExp.Compose((a, b) => a.SupplierID == supplierID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(partCode))
            {
                whereExp = whereExp.Compose((a, b) => a.PartCode.StartsWith(partCode), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(partName))
            {
                whereExp = whereExp.Compose((a, b) => a.PartName.StartsWith(partName), Expression.AndAlso);
            }

            var list = dal.Queryable<T_Material_Parts, T_Supplier>((a, b) => new object[]
            {
                JoinType.Left,a.SupplierID == b.ID,
            })
            .Where(whereExp)
            .OrderBy(a => a.CreateTime, OrderByType.Desc)
            .Select((a, b) => new T_Material_Parts()
            {
                ID = a.ID,
                CompanyID = a.CompanyID,
                PartCode = a.PartCode,
                PartName = a.PartName,
                SupplierID = a.SupplierID,
                SupplierName = b.SupplierName,
                RoHSTestReportID = a.RoHSTestReportID,
                MCDID = a.MCDID,
                GreenStatus = a.GreenStatus,
                DataStatus = a.DataStatus,
                CreateTime = a.CreateTime
            })
            .ToPageList(page.PageIndex, page.PageSize, ref totalCount);
            page.DataCount = totalCount;
            return new PageData<T_Material_Parts>() { Data = list, Page = page };
        }

        public bool Update(T_Material_Parts model)
        {
            return dal.Updateable<T_Material_Parts>(p => new T_Material_Parts()
            {
                PartCode = model.PartCode,
                PartName = model.PartName,
                SupplierID = model.SupplierID,
                GreenStatus = model.GreenStatus,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
